# -*- coding: UTF-8 -*-
import json

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from roles.rolesDAO import userDAO

rolesDao = userDAO()
#ajax接口，登录页面调用，用来验证用户名密码
@csrf_exempt
def loginAction(request):
    """认证机制做的非常简单"""
    if request.is_ajax():
        strUsername = request.POST.get('username')
        strPassword = request.POST.get('password')
    else:
        strUsername = request.POST['username']
        strPassword = request.POST['password']

    #服务端二次验证参数
    print(strUsername)
<<<<<<< HEAD
    user = rolesDao.getUserInfo(strUsername)
    userName = user['userName']
    password = user['passWord']
    groupId = user['groupId']
=======
>>>>>>> 03b0cbb3079f9b560266e5d23952e075f9348e64

    if strUsername is None or strPassword is None:
        result = {'status':2, 'msg':'登录用户名或密码为空，请重新输入!', 'data':''}
        return HttpResponse(json.dumps(result), content_type='application/json')

    user = rolesDao.getUserInfo(strUsername)
    if user:
        userName = user['userName']
        password = user['passWord']
        groupId = user['groupId']
    if strUsername == userName and strPassword == password:
        request.session['login_username'] = strUsername
        result = {'status':0, 'msg':'ok', 'data':groupId}
    else:
        result = {'status':1, 'msg':'用户名或密码错误，请重新输入！', 'data':''}
    return HttpResponse(json.dumps(result), content_type='application/json')